<?php

class EventTube{

    static private $events = array();
    static private $locked = array();
    static public function addEvent($region, $obj){
        if(isset(EventTube::$locked[$region]) && EventTube::$locked[$region] == true) return;
        EventTube::$events[$region][] = $obj;
    }
    
    static public function addMenu($region, $array, $jsplugin){
        if(isset(EventTube::$locked[$region]) && EventTube::$locked[$region] == true) return;
        if(!is_array($array) && DEBUG) die("EventTube::addMenu - O menu deve ser um array");
        if(!isset(EventTube::$events['event_tube_menu'][$region])){
            EventTube::$events['event_tube_menu'][$region] = array();
        }
        
        $i = count(EventTube::$events['event_tube_menu'][$region]);
        EventTube::$events['event_tube_menu'][$region][$i]['menu']   = $array;
        EventTube::$events['event_tube_menu'][$region][$i]['plugin'] = $jsplugin;
    }
    
    static public function lockRegion($region){
        EventTube::$locked[$region] = true;
    }
    
    static public function removeItemFromMenu($region, $nome_item){
        if(!isset(EventTube::$events['event_tube_menu'][$region])) return;
        $menu = EventTube::$events['event_tube_menu'][$region];
        if(empty($menu)) return;
        foreach($menu as $cod => $mn){
            foreach($mn['menu'] as $name => $arr){
                if($nome_item == $name){
                    unset($menu[$cod]['menu'][$name]);
                }
            }
        }
        EventTube::$events['event_tube_menu'][$region] = $menu;
        
    }
    
    static public function clearRegion($region){
        if(isset(EventTube::$events[$region])) 
            unset(EventTube::$events[$region]);
        if(isset(EventTube::$events['event_tube_menu'][$region])) 
            unset(EventTube::$events['event_tube_menu'][$region]);
        if(isset(EventTube::$locked[$region]))
            unset(EventTube::$locked[$region]);
    }

    static public function getRegion($region){
        
        if(isset(EventTube::$events['event_tube_menu'][$region])){
            if(!isset(EventTube::$events[$region])) EventTube::$events[$region] = array();
            foreach(EventTube::$events['event_tube_menu'][$region] as $ev){
                $plugin   = $ev['plugin'];
                $menu     = $ev['menu'];
                $obj      = new Object();
                $mn = $obj->LoadJsPlugin($plugin, 'mn');
                $mn->imprime();
                array_unshift(EventTube::$events[$region], $mn->draw($menu));
            }
        }
        
        if(array_key_exists($region, EventTube::$events))
            return EventTube::$events[$region];
    }
}

?>